package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.UserDao;
import entity.UserEntity;

public class UserDaoImpl implements UserDao {

	@Override
	public void add(UserEntity userName) {
		Connection conn = DOBC.getConn();
		String sql = "insert into servlet(user_Name,pass_word,phone,delete_Logo)values(?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName.getUserName());
			ps.setString(2, userName.getPassword());
			ps.setString(3, userName.getPhone());
			ps.setString(4, userName.getDeleteLogo() == null ? "N" : userName.getDeleteLogo());
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
	}

	@Override
	public void update(UserEntity userName) {
		Connection conn = DOBC.getConn();
		String sql = "update servlet set user_name=? where id=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName.getUserName());
			ps.setLong(2, userName.getId());
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
	}

	@Override
	public List<UserEntity> listAll() {
		Connection conn = DOBC.getConn();
		String sql = "Select * from servlet";
		PreparedStatement ps = null;
		List<UserEntity> list = new ArrayList();
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int i = 1;
				UserEntity entity = new UserEntity();
				entity.setId(rs.getLong(i++));
				entity.setUserName(rs.getString(i++));
				entity.setPassword(rs.getString(i++));
				entity.setPhone(rs.getString(i++));
				entity.setCreationTime(rs.getString(i++));
				entity.setUpdateTime(rs.getString(i++));
				entity.setDeleteLogo(rs.getString(i++));
				list.add(entity);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);

		return list;
	}

	@Override
	public UserEntity getById(Long id) {
		Connection conn = DOBC.getConn();
		String sql = "select id,user_name,phone,pass_word,creation_time,update_time,delete_logo from servlet where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				int i = 1;
				UserEntity userEntity = new UserEntity();
				userEntity.setId(rs.getLong(i++));
				userEntity.setUserName(rs.getString(i++));
				userEntity.setPassword(rs.getString(i++));
				userEntity.setPhone(rs.getString(i++));
				userEntity.setCreationTime(rs.getString(i++));
				userEntity.setUpdateTime(rs.getString(i++));
				userEntity.setDeleteLogo(rs.getString(i++));
				return userEntity;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return null;
	}

	@Override
	public void delete(Long id) {
		Connection conn = DOBC.getConn();
		String sql = "update servlet set delete_logo=? where id=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "Y");
			ps.setLong(2, id);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
	}
		
	

}
